﻿using System;
using System.IO;

using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;

using Xc.Views.Core;

namespace Xc.Views.WebBooster
{
    public abstract class WebApplicationBooster<TAppSetting,TApplicationDbContext>: ApplicationBooster<TAppSetting, TApplicationDbContext> where TAppSetting:class,new() where TApplicationDbContext:DbContext 
    {
        /// <summary>
        /// 添加SWAGGER API说明
        /// </summary>
        /// <param name="serviceDescriptors"></param>
        protected IServiceCollection AddSwaggerApi(IServiceCollection serviceDescriptors)
        {
            serviceDescriptors.AddSwaggerGen(c =>
            {
                var files = Directory.GetFiles(Path.GetDirectoryName(this.GetType().Assembly.Location), "*.xml");
                foreach (var item in files)
                {
                    c.IncludeXmlComments(item);
                }
                c.SwaggerDoc(ApplicationVersion, new OpenApiInfo { Title = ApplicationName, Version = ApplicationVersion });
            });
            return serviceDescriptors;
        }
        /// <summary>
        /// 使用Swagger 说明文档
        /// </summary>
        /// <param name="app"></param>
        protected IApplicationBuilder UseSwagger(IApplicationBuilder app)
        {
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.RoutePrefix = String.Empty;
                c.SwaggerEndpoint($"/swagger/{ApplicationVersion}/swagger.json", $"{ApplicationName} {ApplicationVersion}");
            });
            return app;
        }
        /// <summary>
        /// 应用配置文件信息
        /// </summary>
        public override string AppSettingName => "appsettings";
    }
}
